package com.bgds.sys.module.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bgds.sys.common.pagevo.FileResult;
import com.bgds.sys.common.pagevo.PageVo;
import com.bgds.sys.common.pagevo.Result;
import com.bgds.sys.common.utils.FileuploadUtil;
import com.bgds.sys.common.utils.ResultUtil;
import com.bgds.sys.module.entity.DeviceAccuracyMange;
import com.bgds.sys.module.entity.MeasureRecord;
import com.bgds.sys.module.entity.dto.DeviceAccuracyManageDto;
import com.bgds.sys.module.mapper.DeviceAccuracyMangeMapper;
import com.bgds.sys.module.mapper.MeasureRecordMapper;
import com.bgds.sys.module.service.IDeviceAccuracyMangeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bgds.sys.module.service.IMeasureRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  设备精度管理项目服务实现类
 * </p>
 *
 * @author lj
 * @since 2020-02-18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class IDeviceAccuracyMangeServiceImpl extends ServiceImpl<DeviceAccuracyMangeMapper, DeviceAccuracyMange> implements IDeviceAccuracyMangeService {

    @Autowired
    private DeviceAccuracyMangeMapper deviceAccuracyMangeMapper;
    @Autowired
    private FileuploadUtil fileuploadUtil;

    @Autowired
    private IMeasureRecordService iMeasureRecordService;

    @Autowired
    private MeasureRecordMapper measureRecordMapper;
    /**
     * 条件分页查询设备精度管理项目
     * @param pageVo
     * @param name
     * @return
     */
    @Override
    public IPage<DeviceAccuracyMange> listByConditionPage(PageVo pageVo,String name){
        IPage<DeviceAccuracyMange> page=new Page<>(pageVo.getPageNumber(),pageVo.getPageSize());

        return deviceAccuracyMangeMapper.queryByPage(page,name);
    }

    /**
     * 获取所有项目名称
     * @return
     */
    @Override
    public List<String> getAllDeviceAccuracyName(){

      return  deviceAccuracyMangeMapper.queryAllNames();
    }

    /**
     * 添加设备精度管理项目和测量点
     * @param accuracyManageDto
     * @return
     */
    @Override
    public Result<Object> saveUpdate( DeviceAccuracyManageDto accuracyManageDto) {
        Boolean flag=true;
        DeviceAccuracyMange deviceAccuracyMange=accuracyManageDto.getDeviceAccuracyMange();
        //判断删除或者新增
        if(deviceAccuracyMange.getId()==null){
            deviceAccuracyMangeMapper.insertOne(deviceAccuracyMange);
            List<MeasureRecord> measureRecords=new ArrayList<>();
            if(accuracyManageDto.getNames().size()>0 && accuracyManageDto.getNames()!=null){
                for(String str:accuracyManageDto.getNames()){
                    MeasureRecord mr=new MeasureRecord();
                    mr.setMeasurePoint(str);
                    mr.setAccuracyMangeId(deviceAccuracyMange.getId());
                    measureRecords.add(mr);
                }
                flag=iMeasureRecordService.saveBatch(measureRecords);
            }
           //修改
        }else{
            saveOrUpdate(deviceAccuracyMange);
            //更新项目管理监测点
            //1.删除原监测点
             measureRecordMapper.delete(new QueryWrapper<MeasureRecord>().eq("accuracy_mange_id",deviceAccuracyMange.getId()));
            //2.添加新监测点
            if(deviceAccuracyMange.getId()>0 && deviceAccuracyMange.getId()!=null){
                List<MeasureRecord> measureRecords=new ArrayList<>();
                if(accuracyManageDto.getNames().size()>0 && accuracyManageDto.getNames()!=null){
                    for(String str:accuracyManageDto.getNames()){
                        MeasureRecord mr=new MeasureRecord();
                        mr.setMeasurePoint(str);
                        mr.setAccuracyMangeId(deviceAccuracyMange.getId());
                        measureRecords.add(mr);
                    }
                    flag=iMeasureRecordService.saveBatch(measureRecords);
                }

            }else{
                return new ResultUtil<Object>().setErrorMsg("修改失败!");
            }
        }
        if(!flag){
            return new ResultUtil<Object>().setErrorMsg("保存失败!");
        }
        return new ResultUtil<Object>().setSuccessMsg("保存成功!");
    }
}
